<template>
  <div>
      <el-card class="mb">
          <el-input placeholder="请输入关键词搜索" v-model="queryform.username"
              style="width: 200px;margin-right: 10px;">
          </el-input>
          <el-button type="primary" @click="search()" plain>查询</el-button>
          <el-button type="info" @click="research()" plain>重置</el-button>
          <el-button type="warning" style="float:right" @click="add()" plain>新增</el-button>
      </el-card>
      <el-card>
          <div class="table">
              <el-table :data="tableData" strip>
                  <el-table-column type="index" label="序号" width="70" align="center" sortable></el-table-column>
                  <el-table-column prop="username" label="用户名称"></el-table-column>
              
                  <el-table-column prop="type" label="用户角色">
                    <template slot-scope="scope">
                        <div v-if="scope.row.type ==1">管理员</div>
                        <div v-if="scope.row.type ==2">成果管理员</div>
                        <div v-if="scope.row.type ==3">教师</div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="icon" label="头像">
                    <template slot-scope="scope">
                        <el-image style="width: 40px; height: 40px; border-radius: 50%" v-if="scope.row.icon"
                        :src="scope.row.icon" :preview-src-list="[scope.row.icon]"></el-image>

                    </template>

                  </el-table-column>

                  <el-table-column prop="createTime" label="创建时间"></el-table-column>


                  <el-table-column label="操作" align="center" width="350">
                      <template v-slot="scope">
                          <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
                          <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
                      </template>
                  </el-table-column>
                </el-table>

              <div class="pagination">
                  <el-pagination background @current-change="handleCurrentChange" :current-page="queryform.current"
                      :page-sizes="[5, 10, 20]" :page-size="queryform.pageSize" layout="total, prev, pager, next"
                      :total="total">
                  </el-pagination>
              </div>
          </div>

      </el-card>

      <el-dialog title="用户管理" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
          <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">


              <el-form-item label="用户名称" prop="gname">
                  <el-input v-model="form.username" placeholder="用户名称"></el-input>
              </el-form-item>
              <el-form-item label="用户密码" prop="password"  >
                  <el-input v-model="form.password" placeholder="用户密码" type="password"></el-input>
              </el-form-item>
              <el-form-item label="头像" prop="gname">
                <el-upload action="/oss"  :headers="{ Authorization:'Bearer ' + token }" 
                     :on-success="handleFileSuccess">
                       <img v-if="this.form.icon" :src="this.form.icon" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
              </el-form-item>
          </el-form>

          <div slot="footer" class="dialog-footer">
              <el-button @click="fromVisible = false">取 消</el-button>
              <el-button type="primary" @click="save">确 定</el-button>
          </div>
      </el-dialog>
  </div>
</template>
<script>
import service from '@/utils/request';

export default {
  data() {
      return {
          fileList: [],
          teacherlist:[],
          tableData: [],  // 所有的数据
          current: 1,   // 当前的页码
          pageSize: 10,  // 每页显示的个数
          total: 0,
          username: null,
          token: "",
          rolelist:[],
          name: null,
          fromVisible: false,
          avatar: "",
          queryform: {
              keyword: "",
              current: 1,
              tid: 0,
              pageSize: 10
          },
          license: "",
          form: {
              avatar: "",
              license: ""
          },
          user: {},
          rules: {
              cname: [
                  { required: true, message: '请输入名称', trigger: 'blur' },
              ],
          },
          ids: [],
          businessList: [],
          categoryList: [],
          hotellist: [],
          sjlist: [],
      }
  },
  mounted() {
      this.token = window.localStorage.getItem("token")
    
      console.log(this.user)

      this.getlist()
      if (this.user.role == 'ADMIN') {
          this.getall()
      }
  },
  methods: {
      shenhe(id){

      },
      async selectca(e) {
          console.log(e)
          // this.form.categoryId=""
          const res = await service.get("/hotal/typeByhotel?id=" + e);
          console.log(res)
          this.categoryList = res.data

      },
      selectcaf(e) {
          this.$forceUpdate()
      },
      handleFileSuccess(e) {
          this.form.icon = e.data
      },
      async getall() {
          const res = await service.get("/teacher/all");
          console.log(res)
          this.teacherlist = res.data
      },
      guidang(id){
          this.$confirm('是否确认归档？, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(async () => {
              const res = await service.get("/subject/guidang?id=" + id)
              console.log(res)
              if (res.code == 200) {
                  this.$message({
                      type: 'success',
                      message: '归档成功!'
                  });
                  this.getlist()
              }

          }).catch(() => {
              this.$message({
                  type: 'info',
                  message: '已取消删除'
              });
          });

      },
      del(id) {
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(async () => {
              const res = await service.delete("/sys/user/" + id)
              console.log(res)
              if (res.code == 200) {
                  this.$message({
                      type: 'success',
                      message: '删除成功!'
                  });
                  this.getlist()
              }

          }).catch(() => {
              this.$message({
                  type: 'info',
                  message: '已取消删除'
              });
          });
      },
      force(e) {
          this.form.timeRange = e;
          this.$forceUpdate();
      },
      handleEdit(row) {
          this.form = row;
this.avatar=row.avatar
          this.form.teacherid = row.teacherid
          if (this.user.role == 'ADMIN') {
          this.getall()

          }

          this.fromVisible = true
      },
      search() {
          this.queryform.current = 1;
          this.queryform.pageSize = 10;
          this.getlist()
      },
      research() {
          this.queryform.username = ""
          this.queryform.current = 1;
          this.queryform.pageSize = 10;
          this.getlist()

      },
      async getlist() {
          const res = await service.get("/sys/user/page?type=2", { params: this.queryform });
          console.log(res);
          if (res.code == 200) {
              this.tableData = res.data.records;
              this.total = res.data.total;
          }


      },
      add() {
          this.avatar = ""
          this.license = "";
          this.fileList = [];
          this.form = {
            icon:""
          }
          this.fromVisible = true

      },
      handleLicenseSuccess(e) {

      },
      async save() {
        this.form.roleIds=[2]
        this.form.type=2 
if(!this.form.id){
    const res = await service.post("/sys/user", this.form);
          if (res.code == 200) {
              this.fromVisible = false
              this.$message.success("操作成功")
              this.getlist()
          }

}else{
    const res = await service.put("/sys/user", this.form);
          if (res.code == 200) {
              this.fromVisible = false
              this.$message.success("操作成功")
              this.getlist()
          }

}
    


      },
      handleCurrentChange(e) {

      },
      handleAvatarSuccess(e) {
          console.log(e)
          this.avatar = e.data;
          console.log(this.form.avatar)
      },
      handleAvatarSuccess1(e) {

          this.license = e.data;
      }
  }
}
</script>
<style scoped>
.mb {
  margin-bottom: 10px;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
